﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using WCSY.BLL;
using WCSY.MODEL;

public partial class AuxiliaryManagement_BasicDataManagement_Initialbudget_InitialBuList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
            fg_InitialBuMstManager bum = new fg_InitialBuMstManager();
            gvBind(bum.GetAllList().Tables[0]);
        }
    }

    public void gvBind(DataTable gvdt) {
        this.GridView1.DataSource = gvdt;
        this.GridView1.DataBind();
    }
    /// <summary>
    /// 查询数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSec_Click(object sender, EventArgs e)
    {
        string sysno = txtsysno.Text;
        string uyear = txtUyear.Text;
        string period = ddlperiod.SelectedValue;
        string strwhere = " 1= 1 ";
        if(!sysno.Equals("")){
            strwhere += " and sysno like '%"+sysno+"%' ";
        }
        if(!uyear.Equals("")){
            strwhere += " and uyear = '" + uyear + "' ";
        }
        if(!period.Equals("-1")){
            strwhere += " and period = '" + ddlperiod.SelectedItem.Text + "'";
        }
        fg_InitialBuMstManager bum = new fg_InitialBuMstManager();
        gvBind(bum.GetList(strwhere).Tables[0]);
    }
    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        //遍历每一行
        List<string> sqlList = new List<string>();
        fg_InitialBuMstManager mstm = new fg_InitialBuMstManager();
        fg_InitialBuDetManager detm = new fg_InitialBuDetManager();
        foreach (GridViewRow row in this.GridView1.Rows)
        {   //如果是数据行
            if (row.RowType == DataControlRowType.DataRow)
            {
                //判断复选框是否选中
                CheckBox cb = row.FindControl("orderCheckBox") as CheckBox;
                //判断是否选中没有
                if (cb.Checked)
                {
                    string id = this.GridView1.DataKeys[row.RowIndex]["sysno"].ToString();

                    fg_InitialBuMst mst = mstm.GetMODEL(id);
                    if (mst.isActive != null && mst.isActive==1)
                    {
                        this.RegisterStartupScript("alert", "<script>alert('预算期间已提交或审批执行，不允许删除！')</script>");
                        return;
                    }
                    sqlList.Add(mstm.Delete(id));
                    sqlList.Add(detm.Delete(id));

                    //Response.Write("<script>alert('删除成功！');</script>");
                }
            }
        }
        try
        {
            string suc = tool.executeSqlTran(sqlList);
            if (!suc.Equals("suc"))
            {
                throw new Exception(suc);

            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
            error = error.Replace("\r\n", "");
            error = error.Replace("\n", "");
            error = error.Replace("\r", "");
            error = error.Replace("'", "");
            this.RegisterStartupScript("alert", "<script>alert('删除失败，请重新选择！" + error + "')</script>");
            return;

        }
        this.RegisterStartupScript("alert", "<script>alert('删除成功！')</script>");
        fg_InitialBuMstManager bum = new fg_InitialBuMstManager();
        gvBind(bum.GetAllList().Tables[0]);
    }

    protected void btnActive_Click(object sender, EventArgs e)
    {
        //遍历每一行
        List<string> sqlList = new List<string>();
        fg_InitialBuMstManager mstm = new fg_InitialBuMstManager();
        
        foreach (GridViewRow row in this.GridView1.Rows)
        {   //如果是数据行
            if (row.RowType == DataControlRowType.DataRow)
            {
                //判断复选框是否选中
                CheckBox cb = row.FindControl("orderCheckBox") as CheckBox;
                //判断是否选中没有
                if (cb.Checked)
                {
                    string id = this.GridView1.DataKeys[row.RowIndex]["sysno"].ToString();

                    fg_InitialBuMst mst = mstm.GetMODEL(id);
                    if (mst.isActive != null && mst.isActive == 1)
                    {
                        this.RegisterStartupScript("alert", "<script>alert('预算期间已执行！')</script>");
                        return;
                    }
                    mst.isActive = 1;
                    mst.activeDate = DateTime.Now;
                    string user = "";
                    if(Session["userName"]!=null){
                    user = Session["userName"].ToString();
                    }
                    mst.activepsn = user;
                    sqlList.Add(mstm.Update(mst));
                    
                    //Response.Write("<script>alert('删除成功！');</script>");
                }
            }
        }
        try
        {
            string suc = tool.executeSqlTran(sqlList);
            if (!suc.Equals("suc"))
            {
                throw new Exception(suc);

            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
            error = error.Replace("\r\n", "");
            error = error.Replace("\n", "");
            error = error.Replace("\r", "");
            error = error.Replace("'", "");
            this.RegisterStartupScript("alert", "<script>alert('执行失败，请重新选择！" + error + "')</script>");
            return;

        }
        this.RegisterStartupScript("alert", "<script>alert('执行成功！')</script>");
        fg_InitialBuMstManager bum = new fg_InitialBuMstManager();
        gvBind(bum.GetAllList().Tables[0]);
    }
    //绑定行事件
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "if(this!=prevselitem){this.style.backgroundColor='#Efefef'}");//当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseout", "if(this!=prevselitem){this.style.backgroundColor='#ffffff'}");//当鼠标移开时还原背景色
            e.Row.Attributes.Add("onclick", "this.cells[0].childNodes[0].click();");//selectx(this);this.cells[1].childNodes[0].click();
            e.Row.Attributes.Add("ondblclick", "rowdblClick("+e.Row.Cells[2].Text+")");

        }
    }
}
